package com.newmainsoftech.dao.entity;

import java.util.Comparator;

/**
 * Default implementation class of <code>Comparator</code> interface for comparison of 
 * <code>{@link EntityIdentifier}</code> objects.<br /> 
 *   
 * @author Arata Y.
 *
 * @param <E>
 */
public class EntityIdentifierComparator<E extends EntityIdentifier> implements Comparator<E> {
	/**
	 * Compare 2 <code>EntityIdentifier</code> objects.<br />
	 * This default implementation uses <code>{@link EntityIdentifier#comp(Object)}</code> method for 
	 * comparison.
	 * 
	 * @param o1 
	 * @param o2 
	 * @return 0 when 2 objects are considered as identical, negative value when obj is considered as 
	 * bigger or obj should come later, positive value when obj is considered as smaller or should come 
	 * earlier.
	 */
	@Override
	public int compare( final E o1, final E o2) {
		if ( EntityIdentifier.class.isInstance( o2)) {
			return ((EntityIdentifier)o2).comp( o1);
		}
		else if ( o1 == null) return 0;
		return 1;
	}
}